package com.juyu.miliao.authentication.security;

import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.security.exception.IllegalPasswordFormatException;
import com.juyu.miliao.common.security.exception.IllegalUsernameFormatFormatException;
import com.juyu.miliao.common.security.exception.IllegalVerificationCodeFormatException;
import com.juyu.miliao.common.util.IpUtil;
import com.juyu.miliao.common.util.VerifyUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 手机号注册
 *
 * @author zc
 */
public class PhoneNumberRegisterFilter extends AbstractAuthenticationProcessingFilter {

    public PhoneNumberRegisterFilter(String filterProcessesUrl) {
        super(new AntPathRequestMatcher(filterProcessesUrl, "POST"));
    }

    public PhoneNumberRegisterFilter() {
        super(new AntPathRequestMatcher("/register/phoneNumber", "POST"));
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        //验证手机号
        String username = request.getParameter("username");
        if (username == null)
            throw new IllegalUsernameFormatFormatException();
        username = username.trim();
        if (!username.matches("^[0-9]{11}$"))
            throw new IllegalUsernameFormatFormatException("username:" + username);
        //验证密码
        String password = request.getParameter("password");
        if (password == null)
            throw new IllegalPasswordFormatException("password:" + password);
        password = password.trim();
        if (!password.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$"))
            throw new IllegalPasswordFormatException();
        //验证昵称
        String nickName = request.getParameter("nickName");
        if (nickName == null || nickName.trim().isEmpty())
            throw new IllegalVerificationCodeFormatException("nickName:" + nickName);
        //验证性别
        String sex = request.getParameter("sex");
        if (sex == null)
            throw new IllegalPasswordFormatException("sex:" + sex);
        sex = sex.trim();
        if (!sex.matches("[1,2]"))
            throw new IllegalPasswordFormatException("sex:" + sex);
        //验证 头像
        String icon = request.getParameter("icon");

        //验证请求头
        CommonHead com = VerifyUtil.verifyCommonHead(request);


        //获取ip
        String ip = IpUtil.getIPv4Address(request);

        PhoneNumberRegisterToken authRequest = new PhoneNumberRegisterToken(username, password, nickName,
                Integer.parseInt(sex), icon, ip, com);
        return this.getAuthenticationManager().authenticate(authRequest);
    }

}
